define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'machine/index' + location.search,
                    add_url: 'machine/add',
                    edit_url: 'machine/edit',
                    del_url: 'machine/del',
                    multi_url: 'machine/multi',
                    import_url: 'machine/import',
                    table: 'machine',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'specifications', title: __('Specifications'), operate: 'LIKE'},
                        {field: 'punlic_key', title: __('Punlic_key'), operate: 'LIKE'},
                        {field: 'private_key', title: __('Private_key'), operate: 'LIKE'},
                        {field: 'IP', title: __('Ip'), operate: 'LIKE'},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    var average = $("#average");
    var machine_num = $("#machine_num");
    var machine_hnt_num = $("#machine_hnt_num");

    machine_hnt_num.bind("input propertychange",function(){
        average.val(($(this).val()/machine_num.val()).toFixed(2));
    });

    average.bind("input propertychange",function(){
        machine_hnt_num.val($(this).val() * machine_num.val().toFixed(2));
    });

    $("form").on("submit", function (e) {
        e.preventDefault();
        $.ajax({
            url: $("form").attr("action"),
            type: "POST",
            dataType: "json",
            data: $(this).serialize(),
            success: function (ret) {
                if (ret.hasOwnProperty("code")) {
                    var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : "";
                    if (ret.code === 1) {
                        Toastr.success(msg ? msg : __("Wipe cache completed"));
                    } else {
                        Toastr.error(msg ? msg : __("Wipe cache failed"));
                    }
                } else {
                    Toastr.error(__("Unknown data format"));
                }
            }, error: function () {
                Toastr.error(__("Network error"));
            }
        });
        return false;
    });
    return Controller;
});